

local M={}
local IsClient = game:GetService("RunService"):IsClient();

local WeaponFolder=_G.CommonOHOH:GetPath_Loop("ReplicatedStorage.Resource.Gloves")
local WeapCfg=_G.CommonOHOH.config.Weapon


local gUpgradePowerBoost=_G.CommonOHOH.config.Game.WeaponUpgradePowerBoost
local gUpgradeNeedBoost=_G.CommonOHOH.config.Game.WeaponUpgradeNeedBoost


function GetPower(base,level)
    -- print("base,lv,boost-A->",base,level,gUpgradePowerBoost)
    return  math.ceil(base*(1+(level-1)*gUpgradePowerBoost))
    -- return cfg.damge+cfg.damge*0.1*lv
end

function UpgradeNeed(base,level)
    -- print("base,lv,boost-B->",base,level,gUpgradeNeedBoost)
    return   math.ceil(base*(1+(level-1)*gUpgradeNeedBoost)),"base_diamond"

    -- cfg.baseUpgrade+cfg.baseUpgrade*0.1*(lv-1),"base_diamond"
end


--获取武器相关的美术资源
function M.GetWeaponArt(id)

    local cid,lv=M.GetCfgIdAndLevel(id)
    return M.GetWeaponArtByCid(cid)
end


function M.GetWeaponArtByCid(cid)
    local perfab=WeapCfg[cid].perfab
    return {
        -- 箭
        Arrow=_G.CommonOHOH:GetPath_Loop(perfab..".Arrow",WeaponFolder),
        --弓
        Arch=_G.CommonOHOH:GetPath_Loop(perfab..".Part",WeaponFolder),
        --图片
        Image=_G.CommonOHOH:GetPath_Loop(perfab..".Image",WeaponFolder).Value,

        Animation={
            equip=_G.CommonOHOH:GetPath_Loop(perfab..".equip",WeaponFolder),
            attack=_G.CommonOHOH:GetPath_Loop(perfab..".attack",WeaponFolder),
        },
        Audio={
            attack=_G.CommonOHOH:GetPath_Loop(perfab..".attackSound",WeaponFolder),
        }
    }

end

function M.GetNamebyCid(cid)

    if IsClient then
        return  _G.LanguageHelper:GetText(WeapCfg[cid].name)
    else
        return WeapCfg[cid].name
    end

end

function M.GetTitlebyCid(cid)
    if IsClient then
        return  _G.LanguageHelper:GetText(WeapCfg[cid].titleId+600)
    else
        return WeapCfg[cid].titleId
    end
end

function M.GetIconByItemId(id)
    local cid,lv=M.GetCfgIdAndLevel(id)
    local perfab=WeapCfg[cid].perfab
    return  _G.CommonOHOH:GetPath_Loop(perfab..".Image",WeaponFolder).Value
end



--通过武器Id获取 配置id和等级
function M.GetCfgIdAndLevel(id)
  local list=  string.split(id,"_")
  return tonumber(list[1]),tonumber(list[2])
end

--生成武器的itemId，即武器Id
function M.GetItemId(cid,lv)
    return cid.."_"..lv
end

-- 传入武器Id，返回武器数据
function M.Calculate(id)
    local cid,lv=M.GetCfgIdAndLevel(id)
    local artInfo=M.GetWeaponArt(id)
    local cfg=WeapCfg[cid]
    local upCount,upkey=M.Calculate_upgrade(id)

    return {
        cfgId=cid,--配置Id
        level=lv,--等级
        power=M.Calculate_power(id), --伤害值
        name=M.GetNamebyCid(cid),--名字
        titleName=M.GetTitlebyCid(cid),--称号
        titleId=cfg.titleId,--称号Id
        merge=M.Calculate_merge(),--合成所需数量
        nextPower=M.Calculate_nextPower(id),--下一等级伤害
        icon=artInfo.Image,--图标
        upgrade={[upkey]=upCount},--升级所需材料及数量
        art=artInfo--美术资源
    }
end




--根据武器Id，返回合成所需数量
function M.Calculate_merge(id)
    return 3
end

-- 获取伤害值
function M.Calculate_power(id)
    local cid,lv=M.GetCfgIdAndLevel(id)
    local cfg=WeapCfg[cid]
    assert(cfg,"cfg==nil")

    
    return GetPower(cfg.damge,lv)
    -- return cfg.damge+cfg.damge*0.1*lv
end

--获取下一等级的伤害值
function M.Calculate_nextPower(id)
    local cid,lv=M.GetCfgIdAndLevel(id)
    local cfg=WeapCfg[cid]
    assert(cfg,"cfg==nil")
    return GetPower(cfg.damge,lv+1)
end

function M.Calculate_upgrade(id)
    local cid,lv=M.GetCfgIdAndLevel(id)
    local cfg=WeapCfg[cid]
    assert(cfg,"cfg==nil")
    -- return cfg.baseUpgrade+cfg.baseUpgrade*0.1*(lv-1),"base_diamond"

    return UpgradeNeed(cfg.baseUpgrade,lv)
end

-- 根据战力排序，返回Idlist
function M.SortByPower(idList)
    local powerList={}
    for _,v in pairs(idList) do
        -- print("SortByPower--->",v)
        table.insert(powerList,{
            id=v,
            power=M.Calculate_power(v)
        })
    end
    table.sort(powerList,function(a,b)
        return a.power>b.power
    end)
    local r={}
    for _,v in pairs(powerList) do
        table.insert(r,v.id)
    end
    return r
end


-- 错误码：1：货币不够，2,武器数量不够，3未知原因
--能否升级
function M.IsCanUpgrade(id,dataBase)
    local count=dataBase:GetData("weapon").had[id]
    local mergeCount=M.Calculate_merge(id)
    if count and count>=mergeCount then

        local need,costype=M.Calculate_upgrade(id)
        if need==0 then --存在满级的，不能升级
            -- print("A1")    
            return 3
        end

      
       if  _G.CommonOHOH.load.ItemTool().IsHadEnough(costype,need,dataBase) then
            -- print("A2")    
            return 0,need
       else
        -- print("A3")
            return 1
       end
    else
        -- print("A4")
            return 2
    end
end


--升级
function M.Upgrade(id,dataBase)
    local mergeCount=M.Calculate_merge(id)
    local cid,lv=M.GetCfgIdAndLevel(id)
    local newId=M.GetItemId(cid,lv+1)
    dataBase:GetData("weapon").had[id]= dataBase:GetData("weapon").had[id]-mergeCount
    if  dataBase:GetData("weapon").had[newId] then
        dataBase:GetData("weapon").had[newId]=  dataBase:GetData("weapon").had[newId]+1
    else
        dataBase:GetData("weapon").had[newId]=1
    end
    
    if   dataBase:GetData("weapon").had[id]==0 then
        dataBase:GetData("weapon").had[id]=nil
    end


    --如果装备中的箭被合成，更换装备信息
    local equipId=dataBase:GetData("weapon").equip
    if equipId==id then
        dataBase:GetData("weapon").equip=newId
    end

end



return M